package com.ehomepay.escrow.transition.common.base;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ehomepay.escrow.transition.common.constants.MessageConstants;
import com.ehomepay.escrow.transition.common.enums.ResultCodeEnum;
import com.ehomepay.escrow.transition.common.vo.ResultVO;

/**
 * 
 * @ClassName: BaseController
 * @Description: baseController
 * @author shangxuanc
 * @date 2017年7月31日 下午8:36:48
 */
public class BaseController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 校验参数
	 * 
	 * @param result
	 * @return
	 */
	protected ResultVO checkValid(BindingResult result) {
		List<FieldError> errors = result.getFieldErrors();
		for (FieldError fieldError : errors) {
			logger.info("checkValid->{}", fieldError.getDefaultMessage());
			return this.error(ResultCodeEnum.ERROR.getCode(), fieldError.getDefaultMessage());
		}
		return this.success(ResultCodeEnum.SUCCESS.getCode(), MessageConstants.SUCCESS);

	}

	/**
	 * 
	 * @Description:异常处理
	 * @param e
	 * @return 设定文件
	 * @throws 异常说明
	 */
	@ExceptionHandler(Throwable.class)
	@ResponseBody
	public ResultVO handleException(Throwable e) {
		logger.error("接口调用异常，异常信息为：{}", e);
		return this.error(MessageConstants.NET_OUT_TIME);
	}

	/**
	 * 
	 * @Description: 返回封装,code=500
	 * @param info
	 * @return ResultVO
	 */
	protected ResultVO error(String info) {
		ResultVO vo = new ResultVO();
		vo.setCode(ResultCodeEnum.ERROR.getCode());
		vo.setInfo(info);
		return vo;
	}

	/**
	 * @Description:返回封装，code=500支持自定义res信息
	 * @param info
	 * @param res
	 * @return ResultVO
	 */
	protected ResultVO error(String info, Object res) {
		ResultVO vo = new ResultVO();
		vo.setCode(ResultCodeEnum.ERROR.getCode());
		vo.setInfo(info);
		vo.setData(res);
		return vo;
	}

	/**
	 * @Description: 返回封装，code=0
	 * @param res
	 * @return 设定文件
	 */
	protected ResultVO success(Object res) {
		return success(null, res);
	}

	/**
	 * @Description:自定义提示信息方法
	 * @param code
	 *            返回码{@link ResultCode}
	 * @param info
	 *            页面提示信息
	 * @param res
	 *            数据字段 可以为空
	 * @return ResultVO
	 */
	protected ResultVO message(String code, String info, Object res) {
		ResultVO vo = new ResultVO();
		vo.setCode(code);
		vo.setInfo(info);
		if (null != res) {
			vo.setData(res);
		}
		return vo;
	}

	protected ResultVO message(String code, String info) {
		return this.message(code, info, null);
	}

	/**
	 * @Description: 返回封装，code=200
	 * @param info
	 * @param res
	 * @return 设定文件
	 */
	public ResultVO success(String info, Object res) {
		ResultVO vo = new ResultVO();
		vo.setCode(ResultCodeEnum.SUCCESS.getCode());
		if (info == null) {
			vo.setInfo("success");
		} else {
			vo.setInfo(info);
		}
		if (null != res) {
			vo.setData(res);
		}
		return vo;
	}
}
